package cool.mor.service.impl;

import cool.mor.dao.DepartmentRepository;
import cool.mor.dao.EmployeeRepository;
import cool.mor.dao.RewardsAndPunishmentRepository;
import cool.mor.dto.DepartmentEditorDto;
import cool.mor.dto.PersonnelManagementPageQueryDto;
import cool.mor.dto.PersonnelManagementSalaryQueryDto;
import cool.mor.dto.RewardAndPunishmentRecordDto;
import cool.mor.entity.Department;
import cool.mor.entity.Employee;
import cool.mor.entity.RewardsAndPunishment;
import cool.mor.form.DepartmentEditorForm;
import cool.mor.form.EmployeeForm;
import cool.mor.form.PersonnelManagementPageQueryForm;
import cool.mor.form.RewardAndPunishmentRecordForm;
import cool.mor.service.PersonnelManagementService;
import cool.mor.utils.UUIDGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Ivan
 */
@Service
public class PersonnelManagementServiceImpl implements PersonnelManagementService {
    @Autowired
    EmployeeRepository employeeRepository;
    @Autowired
    DepartmentRepository departmentRepository;
    @Autowired
    RewardsAndPunishmentRepository rewardsAndPunishmentRepository;
    @Override
    public  PersonnelManagementPageQueryDto queryPersonnelManagementDataByPage(PersonnelManagementPageQueryForm personnelManagementPageQueryForm) {
        PersonnelManagementPageQueryDto personnelManagementPageQueryDto = new PersonnelManagementPageQueryDto();
        int i=1;
        /**
         * 在dao中查询出page对象
         *
         */
        if (Integer.parseInt(personnelManagementPageQueryForm.getPage())>0){
            Pageable pageable = PageRequest.of(Integer.parseInt(personnelManagementPageQueryForm.getPage())-i,Integer.parseInt(personnelManagementPageQueryForm.getSize()));
            Page<Employee> employeePage = employeeRepository.findAll(pageable);
            personnelManagementPageQueryDto.setPageData(employeePage);
            personnelManagementPageQueryDto.setCode("200");
            personnelManagementPageQueryDto.setMessage("查找成功");
            return personnelManagementPageQueryDto;
        }
        personnelManagementPageQueryDto.setCode("301");
        personnelManagementPageQueryDto.setMessage("查找失败");
        return personnelManagementPageQueryDto;

    }

    @Override
    public PersonnelManagementSalaryQueryDto queryPersonnelManagementSalary(String employeeId) {
        /**
         * 先查部门基本薪资，再查奖惩总额（根据员工id查询其名下的所有奖惩金额‘score’），两者相加即为薪资总和
         */
        PersonnelManagementSalaryQueryDto personnelManagementSalaryQueryDto = new PersonnelManagementSalaryQueryDto();
        Integer rewardsScoreTotal=0;
        Integer salaryTotal=0;
        Department department = departmentRepository.findById(employeeRepository.findById(employeeId).get().getDeptNo()).get();
        String baseSalary = department.getBaseSalary();
        String departmentName = department.getDepartmentName();
        List<RewardsAndPunishment> allByEmployeeId = rewardsAndPunishmentRepository.findAllByEmployeeId(employeeId);
        for (RewardsAndPunishment rewardsAndPunishment: allByEmployeeId
             ) {
            rewardsScoreTotal+=Integer.parseInt(rewardsAndPunishment.getRewardsAndPunishmentScore());
        }
        salaryTotal=Integer.parseInt(baseSalary)+rewardsScoreTotal;
        personnelManagementSalaryQueryDto.setRewardsAndPunishmentsTotal(rewardsScoreTotal.toString());
        personnelManagementSalaryQueryDto.setSalaryTotal(salaryTotal.toString());
        personnelManagementSalaryQueryDto.setDepartmentName(departmentName);
        personnelManagementSalaryQueryDto.setCode("200");
        personnelManagementSalaryQueryDto.setMessage("succeed");
        return personnelManagementSalaryQueryDto;
    }

    @Override
    public DepartmentEditorDto updateDepartmentEditor(EmployeeForm employeeForm) {
        DepartmentEditorDto departmentEditorDto = new DepartmentEditorDto();
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeForm,employee);
        if (null!=employeeRepository.findById(employee.getId()).get()){
            employeeRepository.save(employee);
            departmentEditorDto.setCode("200");
            departmentEditorDto.setMessage("修改成功");
            return departmentEditorDto;
        }
        departmentEditorDto.setCode("301");
        departmentEditorDto.setMessage("员工id不存在");
        return departmentEditorDto;
    }

    @Override
    public RewardAndPunishmentRecordDto addRewardAndPunishmentRecord(RewardAndPunishmentRecordForm rewardAndPunishmentRecordForm) {
        RewardAndPunishmentRecordDto rewardAndPunishmentRecordDto = new RewardAndPunishmentRecordDto();
        RewardsAndPunishment rewardsAndPunishment = new RewardsAndPunishment();
        BeanUtils.copyProperties(rewardAndPunishmentRecordForm,rewardsAndPunishment);
        rewardsAndPunishment.setId(UUIDGenerator.getUUID());
        rewardsAndPunishmentRepository.save(rewardsAndPunishment);
        rewardAndPunishmentRecordDto.setCode("200");
        rewardAndPunishmentRecordDto.setMessage("奖惩记录添加成功");
        return rewardAndPunishmentRecordDto;
    }


}
